package com.yvan.leto;

public class SegmentHelper {
    public final static String SPRING_BOOT = "springboot";
    public final static String LOGBACK = "logback";
    public final static String JSON = "json";
    public final static String XML = "xml";
    public final static String YAML = "yml";
    public final static String TEXT = "txt";
    public final static String PROP = "properties";

    public static String getSegmentName(String segment) {
        return getParty(segment)[0];
    }

    public static String createSegmentName(String name, String type) {
        if (SPRING_BOOT.equals(name)) {
            return SPRING_BOOT;
        }
        if (LOGBACK.equals(name)) {
            return LOGBACK;
        }
        return name + "@" + type;
    }

    public static String getSegmentExt(String segment) {
        return getParty(segment)[1];
    }

    private static String[] getParty(String segment) {
        if (SPRING_BOOT.equals(segment)) {
            return new String[]{SPRING_BOOT, JSON};
        }
        if (LOGBACK.equals(segment)) {
            return new String[]{LOGBACK, XML};
        }
        if (!segment.contains("@")) {
            throw new ArithmeticException("非内定segment \"" + segment + "\"，必须以 segmentName@json 格式存在");
        }
        return segment.split("@");
    }

    public static String getHttpContentType(String segment) {
        if (SPRING_BOOT.equals(segment)) {
            return "application/json";
        }
        if (LOGBACK.equals(segment)) {
            return "text/xml";
        }
        if (JSON.equals(segment)) {
            return "application/json";
        }
        if (XML.equals(segment)) {
            return "text/xml";
        }
        if (YAML.equals(segment)) {
            return "text/yaml";
        }
        if (TEXT.equals(segment)) {
            return "text/plain";
        }
        if (PROP.equals(segment)) {
            return "text/properties";
        }
        return "text/plain";
    }
}
